//
//  ImagePickerViewControler.h
//  habit
//
//  Created by hzm on 16-3-17.
//  Copyright (c) 2016年 custom. All rights reserved.
//

#import "ImagePickerViewController.h"
#import "HIPImageCropperView.h"
#import "AppMacro.h"
#import "AppUtils.h"


@interface ImagePickerViewController()

@property (nonatomic, assign) BOOL      allowEditing;
@property (nonatomic, assign) CGFloat   cropperRatio;
@property (nonatomic, retain) UIViewController *viewController;
@property (nonatomic, retain) HIPImageCropperView *cropperView;
@property (nonatomic, weak) UIViewController *parentController;
@property (nonatomic, assign) UIImagePickerControllerSourceType sourceType;

@end

@implementation ImagePickerViewController

#pragma mark - 私有方法
-(void )registerNotifaction
{
    [self unRegisterNotifaction];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTapCancelButton1) name:XGHabitReceiveLocalAlarmNotification object:nil];
}

- (void)unRegisterNotifaction
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:XGHabitReceiveLocalAlarmNotification object:nil];
}

- (void)setImagePickerEditing:(BOOL)allowsEditing ratio:(CGFloat)ratio parent:(UIViewController *)parent
{
    _allowEditing = allowsEditing;
    _cropperRatio = ratio;
    _parentController = parent;
    [self registerNotifaction];
}

#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self unRegisterNotifaction];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    _sourceType = picker.sourceType;
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    image = [AppUtils fixImageOrientation:image];
    if (!_allowEditing) {
        [self dismissViewControllerAnimated:YES completion:nil];
        [self didFinishCropperImage:image];
        return;
    }
    [self showCropperView:image];
}

- (void)showCropperView:(UIImage *)image
{
    _viewController = [[BaseViewController alloc] init];
    _cropperView = [[HIPImageCropperView alloc]
                                 initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
                                 cropAreaSize:CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH * _cropperRatio)
                                 position:HIPImageCropperViewPositionCenter];
    [_cropperView setOriginalImage:image];
    [_viewController.view addSubview:_cropperView];
    
    UIButton *finishButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [finishButton setTitle:NSLocalizedString(@"选取", nil) forState:UIControlStateNormal];
    [finishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [finishButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [finishButton sizeToFit];
    [finishButton setFrame:CGRectMake(SCREEN_WIDTH - finishButton.frame.size.width - 10.0,
                                       SCREEN_HEIGHT - finishButton.frame.size.height - 15.0,
                                       finishButton.frame.size.width, finishButton.frame.size.height)];
    [finishButton setAutoresizingMask:(UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin)];
    [finishButton addTarget:self action:@selector(didTapFinishButton:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [cancelButton setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [cancelButton sizeToFit];
    [cancelButton setFrame:CGRectMake(10.0, SCREEN_HEIGHT - cancelButton.frame.size.height - 15.0,
                                      cancelButton.frame.size.width, cancelButton.frame.size.height)];
    [cancelButton setAutoresizingMask:(UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin)];
    [cancelButton addTarget:self action:@selector(didTapCancelButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [_viewController.view addSubview:finishButton];
    [_viewController.view addSubview:cancelButton];
    [_parentController presentViewController:_viewController animated:YES completion:nil];
}

- (void)didTapFinishButton:(id)sender
{
    [self didFinishCropperImage:[_cropperView processedImage]];
    [self unRegisterNotifaction];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didTapCancelButton:(id)sender
{
    if (_sourceType == UIImagePickerControllerSourceTypeCamera) {
        [self unRegisterNotifaction];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else {
        [_parentController dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)didTapCancelButton1
{
    if (_sourceType == UIImagePickerControllerSourceTypeCamera) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }
    else {
        [_parentController dismissViewControllerAnimated:NO completion:nil];
    }
    [self unRegisterNotifaction];
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)didFinishCropperImage:(UIImage *)image
{
    NSLog(@"NOTE:You need implemente the interface 'didFinishCropperImage', or you won't get the result image!");
}

@end
